<?php

/**
 * Vega Framework
 *
 * @url http://vega.com.vn
 * @url http://clip.vn
 */

/**
 * Vega_Request class.
 *
 * A data transfer object.
 * Used to transfer data from Router from Vega_Router to Vega_Module
 */

class Vega_Request
{
    protected $_urlMatches = array();

    protected $_requestUrl = null;

    protected $_moduleName = null;

    protected $_actionName = null;

    protected $_params = array();

    protected $_isDispatched = false;

    /**
     * Returns the request URL
     *
     * @return string
     */
    public function getRequestUrl()
    {
        if (null === $this->_requestUrl)
        {
            $this->setRequestUrl();
        }
        return $this->_requestUrl;
    }

    
    /**
     * Sets the request URL. 
     * If input param is null, guess it base on $_SERVER['REQUEST_URI']
     *
     * @param unknown_type $url
     */
    public function setRequestUrl($url = null)
    {
        if (null === $url)
        {
            $url = $_SERVER['REQUEST_URI'];
            $pos = strpos($url, '?');
            if ((int) $pos > 0)
            {
                $url = substr($url, 0, $pos);
            }
        }
        $this->_requestUrl = $url;
    }

    public function isDispatched()
    {
        return $this->_isDispatched;
    }

    public function setDispatched($dispatched = true)
    {
        if ($dispatched === true)
        {
            $this->_isDispatched = true;
        }
        else
        {
            $this->_isDispatched = false;
        }
        return $this;
    }


    /**
     * Sets module name.
     *
     * @param string $name
     * @return Vega_Request
     */
    public function setModuleName($name)
    {
        $this->_moduleName = $name;
        return $this;
    }

    /**
     * Gets module name
     *
     * @return string
     */
    public function getModuleName()
    {
        return $this->_moduleName;
    }

    /**
     * Sets action name
     *
     * @param string $name
     * @return Vega_Request
     */
    public function setActionName($name)
    {
        $this->_actionName = $name;
        return $this;
    }

    /**
     * Gets action name
     *
     * @return string
     */
    public function getActionName()
    {
        return $this->_actionName;
    }

    /**
     * Returns a element of $_SERVER
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public function getServerValue($key, $default = null)
    {
        $key = strtoupper($key);

        if (!isset($_SERVER[$key]))
        {
            return $default;
        }

        return $_SERVER[$key];
    }

    /**
     * Get a header value.
     *
     * @param string $header. Name of the header
     * @return mixed (false | string)
     */
    public function getHeader($header)
    {
        if (empty($header))
        {
            return false;
        }

        $phpServerKey = 'HTTP_' . strtoupper(str_replace('-', '_', $header));

        if (isset($_SERVER[$phpServerKey]))
        {
            return $_SERVER[$phpServerKey];
        }

        if (function_exists('apache_request_headers'))
        {
            $headers = apache_request_headers();

            if (!empty($headers[$header]))
            {
                return $headers[$header];
            }
        }

        return false;
    }

    /**
     * Get GET VALUE
     *
     * @param mixed $key
     * @param mixed $default Default value
     */
    public function getGet($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_GET;
        }
        return ((isset($_GET[$key])) ? $_GET[$key] : $default);
    }

    /**
     * Get a POST value
     *
     * @param mixed $key
     * @param mixed $default Default value
     * @return mixed
     */
    public function getPost($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_POST;
        }
        return ((isset($_POST[$key])) ? $_POST[$key] : $default);
    }

    /**
     * Get a REQUEST value
     *
     * @param mixed $key
     * @param mixed $default Default value
     * @return mixed
     */
    public function getRequest($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_REQUEST;
        }
        return ((isset($_REQUEST[$key])) ? $_REQUEST[$key] : $default);
    }
	
    /**
     * Get a FILE value
     *
     * @param string $key
     * @return mixed (false | array)
     */
    public function getFile($key)
    {
        if (null === $key)
        {
            return $_FILES;
        }
        return ((isset($_FILES[$key])) ? $_FILES[$key] : false);
    }

    /**
     * Get a COOKIE value
     *
     * @param mixed $key
     * @param mixed $default
     * @return mixed
     */
    public function getCookie($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_COOKIE;
        }
        return ((isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default);
    }
}
